package com.example.merchant.service;

import com.example.merchant.entity.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 创建订单
 */
public class GenerateOrdersService {

    // （1）创建订单
    // 入参，用户对象，List<Commodity>
    // 返回，订单对象
    public Order createOrder(Customer customer, List<Commodity> commodityList) {
        Order order = new Order();

        Random r = new Random(1);
        Coupon coupon = new Coupon();
        int m = r.nextInt(900000) + 100000;

        order.setId(m);
        order.setCustomerName(customer.getName());
        order.setCommodityList(commodityList);
        order.setCustomerPhone(customer.getPhone());
        order.setCustomerId(customer.getId());
        order.setCustomerName(customer.getName());
        return order;
    }

    // （2）支付订单
    // 入参，订单ID，用户ID，Coupon
    // 返回，
    public Order payOrder(Order order, Coupon coupon) {
        Order order1 = new Order();
        order1.setCustomerId(order.getId());
        order1.setCustomerName(order.getCustomerName());
        order1.setCommodityList(order.getCommodityList());
        order1.setCustomerPhone(order.getCustomerPhone());
        order1.setMoney(order.getMoney() - coupon.getMoney());

        return order1;

    }


    /*public ArrayList<Order> orderArrayList() {
        ArrayList<Order> orderArrayList = new ArrayList<>();
        List<Customer> customers = MockDatabase.customerList();
        Commodity commodity = new Commodity();
        commodity.setMoney(200.00);
        //CreateCommodityService c = new CreateCommodityService();
        //c.createCommodity2(2, FruitType.APPLE,"苹果",200.00);
        for (Customer customer : customers) {
            Random r = new Random(1);
            //System.out.println(customer.getMoney());
            //System.out.println(commodity.getMoney());
            if (customer.getMoney() < commodity.getMoney()) {
                //System.out.println(customer.getName() + "用户余额不足");
                continue;
            }
            //  生成订单，打印输出
            if (customer.getMoney() >= commodity.getMoney()) {
                int m = r.nextInt(10);
                Order order = new Order();
                //System.out.println("订单ID："+order.getId()+"\t\t"+"订单金额："+order.getMoney()+"\t\t"+"下单人："+order.getCustomerName());
                orderArrayList.add(order);
            }
        }
        return orderArrayList;
    }*/
}

